<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import AuthService from '../services/AuthService.js'

const router = useRouter()
const username = ref('')
const userEmail = ref('')
const featuredProducts = ref([
  {
    id: 1,
    name: '时尚运动鞋',
    price: 399,
    image: 'https://via.placeholder.com/300x300?text=Sports+Shoes'
  },
  {
    id: 2,
    name: '智能手表',
    price: 899,
    image: 'https://via.placeholder.com/300x300?text=Smart+Watch'
  },
  {
    id: 3,
    name: '无线耳机',
    price: 599,
    image: 'https://via.placeholder.com/300x300?text=Wireless+Headphones'
  },
  {
    id: 4,
    name: '轻薄笔记本',
    price: 5999,
    image: 'https://via.placeholder.com/300x300?text=Thin+Laptop'
  }
])

// 检查登录状态
onMounted(() => {
  checkLoginStatus()
})

// 检查登录状态的函数
const checkLoginStatus = () => {
  if (AuthService.isLoggedIn()) {
    const user = AuthService.getCurrentUser()
    if (user) {
      username.value = user.username
      userEmail.value = user.email
    }
  } else {
    // 如果未登录，跳转到登录页面
    router.push('/shop-login')
  }
}

// 处理退出登录
const handleLogout = () => {
  AuthService.logout()
  router.push('/shop-login')
}

// 查看商品详情
const viewProductDetails = (productId) => {
  router.push(`/product/${productId}`)
}
</script>

<template>
  <div class="shop-home">
    <!-- 导航栏 -->
    <nav class="shop-nav">
      <div class="nav-container">
        <div class="nav-left">
          <div class="shop-logo">
            <svg viewBox="0 0 24 24" width="24" height="24" stroke="#4caf50" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
              <circle cx="9" cy="21" r="1"></circle>
              <circle cx="20" cy="21" r="1"></circle>
              <path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path>
            </svg>
            <span>ShopApp</span>
          </div>
        </div>
        <div class="nav-center">
          <div class="search-bar">
            <input type="text" placeholder="搜索商品..." class="search-input" />
            <button class="search-button">
              <svg viewBox="0 0 24 24" width="18" height="18" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
                <circle cx="11" cy="11" r="8"></circle>
                <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
              </svg>
            </button>
          </div>
        </div>
        <div class="nav-right">
          <button class="nav-button cart-button">
            <svg viewBox="0 0 24 24" width="20" height="20" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
              <circle cx="9" cy="21" r="1"></circle>
              <circle cx="20" cy="21" r="1"></circle>
              <path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path>
            </svg>
            <span>购物车</span>
          </button>
          <div class="user-menu">
            <button class="nav-button user-button">
              <svg viewBox="0 0 24 24" width="20" height="20" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
                <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path>
                <circle cx="12" cy="7" r="4"></circle>
              </svg>
              <span>{{ username }}</span>
            </button>
            <div class="dropdown-menu">
              <button class="dropdown-item">个人中心</button>
              <button class="dropdown-item" @click="handleLogout">退出登录</button>
            </div>
          </div>
        </div>
      </div>
    </nav>

    <!-- 英雄区域 -->
    <section class="hero-section">
      <div class="hero-content">
        <h1>欢迎回来，{{ username }}</h1>
        <p>发现最新的时尚单品和科技产品</p>
        <button class="hero-button">立即购物</button>
      </div>
    </section>

    <!-- 特色商品 -->
    <section class="featured-products">
      <div class="section-header">
        <h2>热门推荐</h2>
        <button class="view-more-button">查看全部</button>
      </div>
      <div class="product-grid">
        <div 
          v-for="product in featuredProducts" 
          :key="product.id" 
          class="product-card"
          @click="viewProductDetails(product.id)"
        >
          <div class="product-image">
            <img :src="product.image" :alt="product.name" />
          </div>
          <div class="product-info">
            <h3 class="product-name">{{ product.name }}</h3>
            <p class="product-price">¥{{ product.price }}</p>
            <button class="add-to-cart-button">加入购物车</button>
          </div>
        </div>
      </div>
    </section>

    <!-- 页脚 -->
    <footer class="shop-footer">
      <div class="footer-content">
        <div class="footer-column">
          <h3>ShopApp</h3>
          <p>为您提供最好的购物体验</p>
        </div>
        <div class="footer-column">
          <h4>快速链接</h4>
          <ul>
            <li>首页</li>
            <li>商品分类</li>
            <li>新品上市</li>
            <li>特惠活动</li>
          </ul>
        </div>
        <div class="footer-column">
          <h4>客户服务</h4>
          <ul>
            <li>联系我们</li>
            <li>常见问题</li>
            <li>退换政策</li>
            <li>隐私政策</li>
          </ul>
        </div>
      </div>
      <div class="footer-bottom">
        <p>&copy; 2024 ShopApp. 保留所有权利。</p>
      </div>
    </footer>
  </div>
</template>

<style scoped>
.shop-home {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 导航栏样式 */
.shop-nav {
  background-color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.nav-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 40px;
  height: 64px;
}

.nav-left .shop-logo {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #4caf50;
  font-size: 20px;
  font-weight: 700;
  cursor: pointer;
}

.nav-center .search-bar {
  display: flex;
  align-items: center;
  background-color: #f5f5f5;
  border-radius: 24px;
  padding: 8px 16px;
  width: 400px;
}

.search-input {
  flex: 1;
  border: none;
  background: transparent;
  outline: none;
  font-size: 14px;
  padding: 6px 0;
}

.search-button {
  background: none;
  border: none;
  color: #666;
  cursor: pointer;
  padding: 6px;
}

.nav-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.nav-button {
  display: flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: none;
  color: #333;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 6px;
  transition: all 0.3s ease;
  font-size: 14px;
}

.nav-button:hover {
  background-color: #f5f5f5;
}

.user-menu {
  position: relative;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  min-width: 160px;
  margin-top: 4px;
  display: none;
}

.user-menu:hover .dropdown-menu {
  display: block;
}

.dropdown-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  padding: 12px 16px;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  transition: all 0.2s ease;
}

.dropdown-item:hover {
  background-color: #f5f5f5;
}

/* 英雄区域样式 */
.hero-section {
  background: linear-gradient(135deg, #4caf50 0%, #45a049 100%);
  color: white;
  padding: 80px 40px;
  text-align: center;
}

.hero-content h1 {
  font-size: 48px;
  margin-bottom: 16px;
  font-weight: 700;
}

.hero-content p {
  font-size: 20px;
  margin-bottom: 32px;
  opacity: 0.9;
}

.hero-button {
  background: white;
  color: #4caf50;
  border: none;
  padding: 16px 32px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 32px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.hero-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(255, 255, 255, 0.3);
}

/* 特色商品样式 */
.featured-products {
  padding: 60px 40px;
  background-color: #f9f9f9;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 32px;
}

.section-header h2 {
  font-size: 28px;
  color: #333;
  font-weight: 600;
}

.view-more-button {
  background: none;
  border: 1px solid #4caf50;
  color: #4caf50;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.view-more-button:hover {
  background-color: #4caf50;
  color: white;
}

.product-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 24px;
}

.product-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: all 0.3s ease;
}

.product-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.product-image {
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.product-card:hover .product-image img {
  transform: scale(1.05);
}

.product-info {
  padding: 16px;
}

.product-name {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.product-price {
  font-size: 18px;
  font-weight: 700;
  color: #4caf50;
  margin-bottom: 12px;
}

.add-to-cart-button {
  width: 100%;
  background-color: #f8f8f8;
  border: 1px solid #ddd;
  padding: 8px;
  border-radius: 6px;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  transition: all 0.3s ease;
}

.add-to-cart-button:hover {
  background-color: #4caf50;
  color: white;
  border-color: #4caf50;
}

/* 页脚样式 */
.shop-footer {
  background-color: #333;
  color: white;
  padding: 40px;
  margin-top: auto;
}

.footer-content {
  display: flex;
  justify-content: space-between;
  margin-bottom: 32px;
}

.footer-column {
  flex: 1;
}

.footer-column h3 {
  font-size: 24px;
  margin-bottom: 16px;
  color: #4caf50;
}

.footer-column h4 {
  font-size: 16px;
  margin-bottom: 16px;
  font-weight: 600;
}

.footer-column p {
  color: #ccc;
  line-height: 1.6;
}

.footer-column ul {
  list-style: none;
  padding: 0;
}

.footer-column ul li {
  margin-bottom: 12px;
  color: #ccc;
  cursor: pointer;
  transition: color 0.3s ease;
}

.footer-column ul li:hover {
  color: #4caf50;
}

.footer-bottom {
  text-align: center;
  padding-top: 24px;
  border-top: 1px solid #444;
  color: #999;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .product-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 768px) {
  .nav-container {
    padding: 0 20px;
  }
  
  .nav-center .search-bar {
    width: 200px;
  }
  
  .hero-content h1 {
    font-size: 36px;
  }
  
  .hero-content p {
    font-size: 18px;
  }
  
  .product-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .footer-content {
    flex-direction: column;
    gap: 24px;
  }
}

@media (max-width: 480px) {
  .nav-container {
    padding: 0 12px;
  }
  
  .nav-center .search-bar {
    display: none;
  }
  
  .hero-section {
    padding: 40px 20px;
  }
  
  .hero-content h1 {
    font-size: 28px;
  }
  
  .featured-products {
    padding: 40px 20px;
  }
  
  .product-grid {
    grid-template-columns: 1fr;
  }
}
</style>